Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Conversation

@suejung-sentry
Copy link
Contributor

@suejung-sentry suejung-sentry commented Nov 11, 2024

The Django postgres ORM is synchronous but the GraphQL resolvers are asynchronous. When any ORM commands are attempted to run directly within a resolver, this causes a runtime error You cannot call this from an async context - use a thread or sync_to_async.

I investigated different ways we could potentially use lint or type-check tools or even just a unit test that could catch these errors before they hit prod. Unfortunately I had trouble getting anything that could work reliably, and plug in with our existing tools (ruff, mypy, pytest). I explored the below:

  • mypy django-stubs plugin, with enable-async-orm-checks = True - https://github.com/typeddjango/django-stubs - running this plugin pointed at our configurations could not resolve our django settings
  • pylint custom plugin (ruff doesn't allow custom plugin) - this required adding support for pylint on top of ruff to get custom plugin functionality - but even trying one out I couldn't get a simple test case to work for our needs
  • unit test that traverses resolver functions - this resulted in too many false positives

So in the end I just added in the off-the-shelf async linter offered by flake8 that can catch common errors with async. Unfortunately it doesn't solve the original reason for this investigation re: Django ORM, but can at least be a backstop for other async related pitfalls.

I've also added this "runbook" for on-call or debugging response related to graphQL/async in api

Closes codecov/engineering-team#2473

@codecov-notifications
Copy link

codecov-notifications bot commented Nov 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codecov
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.03%. Comparing base (70b4692) to head (b8d4986).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #968   +/-   ##
=======================================
  Coverage   96.03%   96.03%           
=======================================
  Files         827      827           
  Lines       19071    19071           
=======================================
  Hits        18315    18315           
  Misses        756      756           
Flag Coverage Δ
unit 92.27% <ø> (ø)
unit-latest-uploader 92.27% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions
Copy link
Contributor

✅ All tests successful. No failed tests were found.

📣 Thoughts on this report? Let Codecov know! | Powered by Codecov

@suejung-sentry suejung-sentry changed the title feat: Add async linting chore: Add async linting Nov 12, 2024
@suejung-sentry suejung-sentry marked this pull request as ready for review November 12, 2024 00:49
ruff.toml Outdated
"E", # pycodestyle - error rules
"F", # pyflakes - general Python errors, undefined names
"I", # isort - import sorting
"PERF", # perflint - https://docs.astral.sh/ruff/rules/
Copy link
Contributor

@ajay-sentry ajay-sentry Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe this should be "performance anti-pattern rules" and the rules link should be above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh woops, I misread what the original comment was trying to convey haha. Fixed!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha no worries

Copy link
Contributor

@ajay-sentry ajay-sentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! Happy to see we didn't have any errors for this guy either 😅

@suejung-sentry suejung-sentry added this pull request to the merge queue Nov 12, 2024
Merged via the queue into main with commit 1af369e Nov 12, 2024
19 checks passed
@suejung-sentry suejung-sentry deleted the sshin/fix/lint branch November 12, 2024 02:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write reference/runbook for codecov-api "sync vs. async" root cause of bugs

3 participants